﻿using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;

namespace Unread.App.Common
{
    public static class DependencyObjectExtensions
    {
        public static T GetFirstChild<T>(this DependencyObject dependencyObject) where T : class
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
            {
                var child = VisualTreeHelper.GetChild(dependencyObject, i);
                if (child.GetType() == typeof(T))
                    return child as T;

                if (VisualTreeHelper.GetChildrenCount(child) > 0)
                {
                    var result = GetFirstChild<T>(child);
                    if (result != null)
                        return result as T;
                }
            }

            return null;
        }
    }
}
